package ThreadTest;
public class TicketTest1 {
    public static void main(String[] args) {
        TicketSell ticketSell = new TicketSell();
        Thread thread1 = new Thread(ticketSell);
        Thread thread2 = new Thread(ticketSell);
        Thread thread3 = new Thread(ticketSell);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
class TicketSell implements Runnable{
    private static int tickets = 100;
    public static int getTickets() {
        return tickets;
    }
    @Override
    public void run() {
        while (true){
            Sell();
        }
    }
    // 隐指定上锁的对象
    public static synchronized void Sell(){ // 静态，同步监视器（锁）不是this，是当前对象的类，即TicketSell
        // public synchronized void Sell(){ // 非静态，同步监视器（锁）是this，即当前对象（类的一个实例对象），即thread1、thread2、thread3，方法错误
        if(tickets > 0){
            System.out.println(Thread.currentThread().getName()+":\t"+tickets + "\t->\t" +(tickets-1));
            tickets--;
        }
    }
}